package chapter6_1_LockSupport;

import java.util.concurrent.locks.LockSupport;

/**
 * LockSupport 类与每个使用它的线程都会关联一个许可证，在默认情况下调用
 * LockSupport 类的方法的线程是不持有许可证的
 *
 * @author huayuhao
 * @date 2020/12/16 16:59
 */
public class LockSupportTest2 {

    public static void main(String[] args) throws InterruptedException {

        Thread thread = new Thread(() -> {
            String name = Thread.currentThread().getName();
            System.out.println(name + " begin park");
            // 一直挂起自己,只有被中断,才会推出循环
            while (!Thread.currentThread().isInterrupted()) {
                LockSupport.park();
            }
            System.out.println(name + " end park");
        }, "A");
        thread.start();
        Thread.sleep(1000);
        System.out.println("主线程准备中断线程" + thread.getName());
        // 中断thread
        thread.interrupt();
    }
}
